/*
 * @Description:
 * @Author: Zzt
 * @Date: 2022-11-03 10:17:20
 * @LastEditTime: 2023-01-11 15:56:49
 */
import {
  SET_SELECT_FOND,
  SET_SELECT_AT,
  SET_SELECT_KEY,
  ADD_FATLIST,
  ADD_TAFLIST,
  GET_CURRENT,
  DEL_FATLIST,
  DEL_TATLIST,
  EDIT_FATLIST,
  EDIT_TAFLIST,
  SAVE_HIERACHY,
  SAVE_BASE
} from '../constants';
import { delItemByIdArray, editItemById, insertItem } from '@/utils/list';

export const MappingRelationReducer = (state, action) => {
  switch (action.type) {
    /**
     * @description: 设置选中全宗
     */
    case SET_SELECT_FOND:
      return {
        ...state,
        selectFond: action.data
      };

    /**
     * @description: 设置选中的档案类型
     */
    case SET_SELECT_AT:
      return {
        ...state,
        selectAT: action.data
      };
    case SET_SELECT_KEY: {
      return { ...state, selectItems: action.data };
    }
    case GET_CURRENT: {
      return { ...state, current: action.data };
    }
    case ADD_FATLIST: {
      return { ...state, FATList: insertItem(action.data, state.FATList) };
    }
    case ADD_TAFLIST: {
      return { ...state, TAFList: insertItem(action.data, state.TAFList) };
    }
    case DEL_FATLIST: {
      return { ...state, FATList: delItemByIdArray(action.id, state.FATList) };
    }
    case DEL_TATLIST: {
      return { ...state, TATList: delItemByIdArray(action.id, state.TATList) };
    }
    case EDIT_FATLIST: {
      const { id, data } = action;
      return { ...state, FATList: editItemById(id, data, state.FATList) };
    }
    case EDIT_TAFLIST: {
      const { id, data } = action;
      return { ...state, TAFList: editItemById(id, data, state.TAFList) };
    }
    case SAVE_HIERACHY: {
      return { ...state, hierachy: action.data };
    }
    case SAVE_BASE: {
      return { ...state, base: action.data };
    }
    default:
      console.log('untouched', action);
      return state;
  }
};
